package com.it.park.controller.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.it.park.client.service.QuestionAnswerService;
import com.it.park.client.service.QuestionService;
import com.it.park.client.service.QuestionTypeService;
import com.it.park.client.service.SensitiveWordService;
import com.it.park.common.Page;
import com.it.park.constant.Constant;
import com.it.park.model.Question;
import com.it.park.model.QuestionAnswer;
import com.it.park.model.QuestionType;
import com.it.park.model.SensitiveWord;
import com.it.park.rms.client.model.User;

@Controller
@RequestMapping("/user/question")
public class QuestionController {
	private String rootPath=Constant.USER_PATH;
	@Autowired
	private QuestionService questionService;
	@Autowired
	private QuestionAnswerService questionAnswerService;
	@Autowired
	private QuestionTypeService questionTypeService;
	@Autowired
	private SensitiveWordService sensitiveWordService;
	private Page page;
	
	@RequestMapping("questionManage")
	public String questionManage(Integer pageNow,HttpServletRequest request,HttpSession session){
			List<Question> questions =	null;
			User user = (User) session.getAttribute("user");
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("userId", user.getId());
			Integer total = questionService.selectTotal(map);
			page = new Page(pageNow,total,10);
			map.put("startPos", page.getStartPos());
			map.put("pageSize", page.getPageSize());
			questions = questionService.selectPagination(map);
			request.setAttribute("total", total);
			request.setAttribute("page", page);
			request.setAttribute("questions", questions);
			List<QuestionType> questionTypes =  questionTypeService.selectAll();
			request.setAttribute("questionTypes", questionTypes);
			return rootPath+"question/questionManage";
	}
	
	
	/**
	 * 删除
	 */
	@RequestMapping("deleteQuestion")
	public String deleteQuestion(Integer questionId){
		questionService.deleteByPrimaryKey(questionId);
		return "redirect:questionManage";
	}
	
	/**
	 * 添加或修改保存
	 */
	@RequestMapping("saveQuestion")
	public String saveQuestion(Question question,HttpSession session){
		User user = (User) session.getAttribute("user");
		if(question.getId() != null){
			questionService.updateByPrimaryKeySelective(question);
		}else{
			//过滤敏感词
			String questionDetail = question.getQuestionDetail();
			List<SensitiveWord> words = sensitiveWordService.selectAll();
			for (SensitiveWord word : words) {
				questionDetail = questionDetail.replaceAll(word.getWord(), "***");
			}
			question.setQuestionDetail(questionDetail);
			question.setUserId(user.getId());
			question.setCreateDate(new Date());
			questionService.insertSelective(question);
		}
		return "redirect:questionManage";
	}
	

	/**
	 *修改页面
	 */
	@RequestMapping("toEditQuestion")
	public String toEditQuestion(Integer questionId,HttpServletRequest request){
		Question question = 	questionService.selectByPrimaryKey(questionId);	
		request.setAttribute("question", question);
		List<QuestionType> questionTypes =  questionTypeService.selectAll();
		request.setAttribute("questionTypes", questionTypes);
		return rootPath+"question/editQuestion";
	}
	
	
	/**
	 * 关闭问题
	 */
	@RequestMapping("closeQuestion")
	public String closeQuestion(Integer questionId){
		Question question = questionService.selectByPrimaryKey(questionId);
		question.setIsFin(0);
		questionService.updateByPrimaryKeySelective(question);
		return "redirect:questionManage";
	}
	
	/**
	 * 回答列表
	 */
	@RequestMapping("showAnswers")
	public String showAnswers(Integer pageNow,Integer questionId,HttpServletRequest request){
		Map<String,Object> map  = new HashMap<String,Object>();
		map.put("questionId",questionId);
		Integer total = questionAnswerService.selectTotal(map);
		page = new Page(pageNow==null?1:pageNow,total,10);
		map.put("startPos", page.getStartPos());
		map.put("pageSize", page.getPageSize());
		List<QuestionAnswer> questionAnswers =  questionAnswerService.selectPagination(map);
		request.setAttribute("total", total);
		request.setAttribute("page", page);
		request.setAttribute("questionAnswers", questionAnswers);
		return rootPath+"question/questionAnswers";
		}
	
	/**
	 * 删除问题对应的回答
	 */
	@RequestMapping("deleteQuestionAnswer")
	public String deleteQuestionAnswer(Integer questionAnswerId,Integer questionId){
		questionAnswerService.deleteByPrimaryKey(questionAnswerId);
		return "redirect:showAnswers?questionId="+questionId;
	}

}
